#include "../test_type.h"
#include "../../src/tools/ShareApi.h"
#include "../../src/thread/SharedPtr.h"

USE_LIBTOOLS;

#ifdef TEST_PROCESS
int main(int argc, char** argv, char** evp)
{
#ifdef WIN32
	ShareApi::PrintDumpInfo(true);
#endif

	printf("current process id = %lu\n", ShareApi::GetCurrentProcessId());
	getchar();

	for (int i = 0; i < argc; i++)
	{
		printf("argv %d:%s\n", i, argv[i]);
	}

	getchar();
	for (int i = 0; evp[i] != NullPtr; i++)
	{
		printf("evp %d:%s\n", i, evp[i]);
	}

	const TCHAR* exename = TEXT("");
#ifdef WIN32
	TCHAR fileName[MAX_PATH];
	HMODULE hModule = GetModuleHandle(NullPtr);
	GetModuleFileName(hModule, fileName, MAX_PATH);
	exename = fileName;
#elif defined(LINUX)
	exename = TEXT("./testmain");
#endif
	if (argc == 1)
	{
		printf("start process\n");
		TCHAR *cline[] = { argv[0], TEXT("subprocess"), NULL };
		if (ShareApi::StartProcess(exename, cline, evp) == FALSE)
		{
			SharedPtr<TCHAR> err(ShareApi::Global_GetLastError(), &ShareApi::Global_FreeMem);
			printf("create process failed:%s\n", err.Get());
		}
		printf("end process\n");
	}

	printf("exit process:%lu\n", ShareApi::GetCurrentProcessId());
	getchar();

	return 0;
}
#endif // TEST_PROCESS